// logs.js
const util = require('../../utils/util.js')

Page({
  data: {
    logs: [],
    activeIndex: 0,
    //当天的信息
    dayList:[],
    list:[],
    sum: [{
        title: "今日番茄次数",
        val: '0'
      }, {
        title: "累计番茄次数",
        val: '0'
      },
      {
        title: "今日专注时长",
        val: '0分钟'
      },
      {
        title: "累计专注时长",
        val: '0分钟'
      },
    ],
    cateArr: [{
      icon: 'gongzuo',
      text: '工作'
    },
    {
      icon: 'xuexi',
      text: '学习'
    },
    {
      icon: 'sikao',
      text: '思考'
    },
    {
      icon: 'yuedu',
      text: '阅读'
    },
    {
      icon: 'yundong',
      text: '运动'
    },
    {
      icon: 'xiezuo',
      text: '写作'
    },
  ]
  },
  onShow() {
    const logs = wx.getStorageSync('logs') || [];
    //今日番茄次数
    let day = 0;
    //累计番茄次数
    let total = logs.length;
    //今日时长
    let dayTime = 0;
    //累计时长
    let totalTime = 0;
    let dayList =[];
    if (logs.length > 0) {
      for (let i = 0; i < logs.length; i++) {
        if (logs[i].date.substr(0, 10) == util.formatTime(new Date).substr(0, 10)) {
          day = day + 1;
          dayTime = dayTime + parseInt(logs[i].time);
          dayList.push(logs[i]);
          this.setData({
            dayList: dayList,
            list:dayList
          })
        }
        totalTime = totalTime + parseInt(logs[i].time);
      }
      this.setData({
        'sum[0].val':day + '次',
        'sum[1].val':total + '次',
        'sum[2].val':dayTime +'分钟',
        'sum[3].val':totalTime + '分钟'
      })
    }
  },
  //今日和历史进行切换
  changeType(e) {
    const index = e.currentTarget.dataset.index;
    if(index == 0) {
      this.setData({
        list:this.data.dayList
      })
    } else if(index == 1) {
      const logs = wx.getStorageSync('logs') || []
      this.setData({
        list:logs
      })
    }
    this.setData({
      activeIndex: index
    })
    // console.log(e.currentTarget.dataset.index);
  }
})